package com.example.web.queue;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.example.web.config.MQConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author yzl
 *
 */
@Slf4j
public class Consumer extends MQConfig {

	public static void main(String[] args) throws JMSException {
		// 创建会话
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// 创建目的地
		Destination destination = session.createQueue(MQConfig.queueName);
		// 创建消费者
		MessageConsumer consumer = session.createConsumer(destination);
		// 接收消息
		consumer.setMessageListener(new MessageListener() {

			@Override
			public void onMessage(Message message) {
				TextMessage textMessage = (TextMessage) message;
				try {
					log.info("接受消息：{}", textMessage.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
		// connection.close();
	}

}
